package com.bloomfiler.demo.controller;

import com.alibaba.fastjson.JSON;
import com.bloomfiler.demo.dao.User;
import lombok.val;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;

/**
 * 限流实验
 */
@RestController
public class RedisStreamLimterController {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    RedisTemplate redisTemplate;

    Logger logger = Logger.getLogger("ddd");
    private DefaultRedisScript<Long> script;

    @GetMapping("/{key}/{name}/{num}")
    public String add1(@PathVariable String name, @PathVariable String key, @PathVariable String num) {
        tokenCheck(key, name, num);

        redisTemplate.opsForValue().set(name + "my", JSON.toJSON(new User(name, num)));
        Object objetc = redisTemplate.opsForValue().get(name + "my");
        System.out.println(objetc.toString());
        return "ok";
    }

    @GetMapping("/limit")
    public String streamLimitInterface() {
        int r = Integer.valueOf(StreamLimit("limit", "30", "1"));
        if (r >0) {

            logger.info(String.valueOf(r));
        }
        return "ok" + r;
    }

    @GetMapping("/newlimit")
    public String newstreamLimitInterface() {
        int r = Integer.valueOf(StreamLimit("limit", "5", "1"));
        if (r < 0) {
            logger.info(String.valueOf(r));
        }
        return "ok" + r;
    }
    @PostConstruct
    public void init() {
        script = new DefaultRedisScript<>();
        script.setResultType(Long.class);
        script.setScriptSource(new ResourceScriptSource(new ClassPathResource("add.lua")));
    }

    public String tokenCheck(String gamekey, String curtime, String num) {

        List<String> keys = new ArrayList();
        keys.add(gamekey);
        ExecutorService e;
        //script:lua脚本
        //KEYS[1] KEYS[2]，是要操作的键，可以指定多个，在lua脚本中通过KEYS[1], KEYS[2]获取
        //ARGV[1] ARGV[2]，参数，在lua脚本中通过ARGV[1], ARGV[2]获取
        RedisSerializer<Integer> redisSerializer = redisTemplate.getValueSerializer();
        Object result = redisTemplate.execute(script, new StringRedisSerializer(), new StringRedisSerializer(), keys, curtime, num);
        System.out.println(result);
        return result.toString();

    }

    @PostConstruct
    public void init1() {
        script = new DefaultRedisScript<>();
        script.setResultType(Long.class);
        script.setScriptSource(new ResourceScriptSource(new ClassPathResource("Limit.lua")));
    }

    public String StreamLimit(String gamekey, String maxStreamLimitNum, String TimeOut) {

        List<String> keys = new ArrayList();
        keys.add(gamekey);
        ExecutorService e;
        //script:lua脚本
        //KEYS[1] KEYS[2]，是要操作的键，可以指定多个，在lua脚本中通过KEYS[1], KEYS[2]获取
        //ARGV[1] ARGV[2]，参数，在lua脚本中通过ARGV[1], ARGV[2]获取
        RedisSerializer<Integer> redisSerializer = redisTemplate.getValueSerializer();
        Object result = redisTemplate.execute(script, new StringRedisSerializer(), new StringRedisSerializer(), keys, maxStreamLimitNum, TimeOut);
        return result.toString();
    }
}
